<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use Storage;
use Illuminate\Support\Str;

/**
 * Class CardsPictureIntroductions
 *
 * @package App\Model
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\CardsPictureIntroductions onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\CardsPictureIntroductions withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\CardsPictureIntroductions withoutTrashed()
 * @mixin \Eloquent
 * @property-read \App\Model\Cards $card
 * @property-read mixed $picture_path
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions query()
 * @property int $id 主键
 * @property int $type 类型 1:自我介绍 2:公司介绍 3:产品介绍
 * @property int $user_id 用户id
 * @property string $title 标题
 * @property string|null $content 内容
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 修改时间
 * @property \Illuminate\Support\Carbon|null $deleted_at 删除时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions wherePicturePath($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\CardsPictureIntroductions whereUserId($value)
 */
class CardsPictureIntroductions extends Model
{
    use SoftDeletes;

    protected $table = 'card_picture_introductions';

    protected $primaryKey = 'id';

    protected $dates = ['deleted_at'];

    protected $fillable = ['user_id', 'type', 'title', 'content', 'picture_path'];

    public function card()
    {
        return $this->belongsTo(Cards::class, 'user_id', 'id');
    }

    public function picturesByCard($card_id, $type)
    {
        return $this->where(['card_id' => $card_id, 'type' => $type])->orderByDesc('created_at')->get();
    }

    public function getPicturePathAttribute($value)
    {
        return json_decode($value);
    }

    public function add(Request $request)
    {
        $data = $request->input();
        //添加图片
        $images = json_encode($data['images']);
        //添加数据
        $this->user_id = $request->user()->id;
        $this->type = $data['type'];
        $this->title = $data['title'];
        $this->content = $data['content'];
        $this->picture_path = $images;
        return $this->save();
    }

    public function edit(Request $request)
    {
        $data = [
            'title' => $request->input('title'),
            'content' => $request->input('content'),
            'picture_path' => json_encode($request->input('images'))
        ];
        return $this->update($data);
    }
}
